package com.itheima.controller;

import com.itheima.service.FileService;
import com.itheima.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/14 22:31
 * @Desc: 文件上传
 */
@RestController
@Slf4j
public class FileController {
    @Autowired
    private FileService fileService;

    /**
     * 获取阿里云OSS里的图片
     *
     * @param name
     * @return
     */
    @GetMapping("/common/download")
    public void download(String name, HttpServletResponse response) throws IOException {
        fileService.download(name, response);
    }

    /**
     * 文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/common/upload")
    public R<String> upload(MultipartFile file) throws IOException {
        log.info("====================文件上传测试======================");
        log.info(file.getOriginalFilename());

        String url = fileService.upload(file);

        log.info("======================文件上传完成======================");
        log.info(url);

        return url != null ? R.success(url) : R.error("文件上传失败");
    }
}
